package com.noyes4j.ota.controller;

import com.noyes4j.common.core.controller.BaseController;
import com.noyes4j.common.core.domain.R;
import com.noyes4j.common.core.page.TableDataInfo;
import com.noyes4j.common.log.annotation.OperLog;
import com.noyes4j.common.log.enums.BusinessType;
import com.noyes4j.common.utils.poi.ExcelUtil;
import com.noyes4j.ota.domain.SysProduce;
import com.noyes4j.ota.service.ISysProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品Controller
 *
 * @author noyes4j
 * @date 2021-01-29
 */
@Controller
@RequestMapping("/ota/produce")
public class SysProduceController extends BaseController
{
    private String prefix = "ota/produce";

    @Autowired
    private ISysProduceService sysProduceService;

    @GetMapping()
    public String produce()
    {
        return prefix + "/produce";
    }

    /**
     * 查询产品列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysProduce sysProduce)
    {
        startPage();
        List<SysProduce> list = sysProduceService.selectSysProduceList(sysProduce);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PostMapping("/export")
    @ResponseBody
    public R export(SysProduce sysProduce)
    {
        List<SysProduce> list = sysProduceService.selectSysProduceList(sysProduce);
        ExcelUtil<SysProduce> util = new ExcelUtil<SysProduce>(SysProduce.class);
        return util.exportExcel(list, "produce");
    }

    /**
     * 新增产品
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品
     */
    @OperLog(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public R addSave(SysProduce sysProduce)
    {
        return toAjax(sysProduceService.insertSysProduce(sysProduce));
    }

    /**
     * 修改产品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysProduce sysProduce = sysProduceService.selectSysProduceById(id);
        mmap.put("sysProduce", sysProduce);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品
     */
    @OperLog(title = "产品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public R editSave(SysProduce sysProduce)
    {
        return toAjax(sysProduceService.updateSysProduce(sysProduce));
    }

    /**
     * 删除产品
     */
    @OperLog(title = "产品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public R remove(String ids)
    {
        return toAjax(sysProduceService.deleteSysProduceByIds(ids));
    }
}